package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ArrangeProgressBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 产业园接待行程
 */
public class AddReceptionTravelActivity extends BaseActivity implements View.OnClickListener {
    private final static int CHOOSE_ADD_CODE = 562;

    private View select_time_lay;
    private View select_address_lay;
    private TextView select_address_tools;
    private TextView select_address_select_tv;//地址选择内容
    private TextView select_time_tools;
    private TextView select_time_select_tv;//时间选择内容


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_reception_travel);
        initData();
        initTitle();
        initView();


    }

    private ArrangeProgressBean.TripListEntity trip = new ArrangeProgressBean.TripListEntity();

    private void initData() {
        if (getIntent().getIntExtra("position", -1) == -1)
            trip = new ArrangeProgressBean.TripListEntity();
        else {
            trip = ArrangeRouteActivity.maxBean.getTrip_list().get(getIntent().getIntExtra("position", -1));
        }
    }

    private void initView() {
        select_time_lay = findViewById(R.id.select_time_lay);
        select_address_lay = findViewById(R.id.select_address_lay);
        select_address_tools = (TextView) select_address_lay.findViewById(R.id.select_tools);
        select_address_select_tv = (TextView) select_address_lay.findViewById(R.id.select_tv);
        select_time_tools = (TextView) select_time_lay.findViewById(R.id.select_tools);
        select_time_select_tv = (TextView) select_time_lay.findViewById(R.id.select_tv);
        select_time_tools.setText(R.string.add_reception_travel_select_time_tools);
        select_address_tools.setText(R.string.add_reception_travel_select_address_tools);
        if (getIntent().getIntExtra("position", -1) != -1) {
            select_time_select_tv.setText(trip.getStart_time());
            select_address_select_tv.setText(trip.getPlace_name());
        }
        if (ArrangeRouteActivity.maxBean.getCanUpdata().equals("0")) {


        } else {
            findViewById(R.id.select_address_lay).setOnClickListener(this);
            findViewById(R.id.select_time_lay).setOnClickListener(this);

        }

    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(R.string.add_reception_travel_title);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        TextView title_right_tv_save = (TextView) findViewById(R.id.tv_right);
        title_right_tv_save.setVisibility(View.VISIBLE);
        title_right_tv_save.setText(R.string.title_right_tv_save);

    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //保存
            case R.id.btn_main_right:
//                MyToast(R.string.title_right_tv_save);
//                trip.set
                String select_time_toolss = select_time_select_tv.getText().toString().trim();
                String select_address_toolss = select_address_select_tv.getText().toString().trim();
                if ("".equals(select_time_toolss)) {
                    MyToast("请填写开始时间");
                    return;
                }
                if ("".equals(select_address_toolss)) {
                    MyToast("请填写地点");
                    return;
                }
                trip.setTotal_time("20");
                trip.setPlace_name(select_address_toolss);
                trip.setStart_time(select_time_toolss);

                if (getIntent().getIntExtra("position", -1) == -1) {
                    ArrangeRouteActivity.maxBean.getTrip_list().add(trip);
                } else {
                    ArrangeRouteActivity.maxBean.getTrip_list().set(getIntent().getIntExtra("position", 0), trip);
                }

                setResult(RESULT_OK);
                finish();
                break;


            //行程地点
            case R.id.select_address_lay:
                intent = new Intent(this, TravelAddressPickActivity.class);
                startActivityForResult(intent, CHOOSE_ADD_CODE);
                break;
            case R.id.select_time_lay:

//                TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
//                    @Override
//                    public void onTimeSelect(Date date, View v) {//选中事件回调
//                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                        String defaultTime = format.format(date);
//                        select_time_select_tv.setText(defaultTime);
//                    }
//                })   //.setTitleSize(16)//标题文字大小
//                        // .setTitleText("预算系数")
//                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
//                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
//                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
//                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
//
//                        .setType(TimePickerView.Type.ALL)//默认全部显示
//                        .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
//                        .setTitleBgColor(getResources().getColor(R.color.white))//标题背景颜色 Night mode
//                        .setBgColor(getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
//                        .setLabel("年", "月", "日", "时", "分", "秒")
//                        .isDialog(true)//是否显示为对话框样式
//                        .gravity(Gravity.BOTTOM)
//                        .build();
//                pvTime.show();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                String str = formatter.format(curDate);
                String[] b = str.split("-");
                Calendar startDate = Calendar.getInstance();
                startDate.set(Integer.parseInt(b[0]), Integer.parseInt(b[1]) - 1, Integer.parseInt(b[2]));
                Calendar endDate = Calendar.getInstance();
                endDate.set(2110, 10, 29);
                TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调

                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        String defaultTime = format.format(date);
                        select_time_select_tv.setText(defaultTime);
                    }
                })
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .setType(TimePickerView.Type.YEAR_MONTH_DAY_HOUR_MIN)//默认全部显示
                        .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                        .setTitleBgColor(getResources().getColor(R.color.white))//标题背景颜色 Night mode
                        .setBgColor(getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
                        .setRangDate(startDate, endDate)//默认是1960-2110年
                        .setDate(startDate)// 默认是系统时间*/
                        .build();
                pvTime.show();

                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CHOOSE_ADD_CODE:
                if (resultCode == RESULT_OK) {
                    String address = data.getStringExtra("address");
                    select_address_select_tv.setText(address);
                    trip.setPlace_id(data.getStringExtra("place"));
                }
                break;

            default:
                break;
        }
    }
}
